/* 
 * Copyright (c) 2022, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.module.oplog.core;

import com.cory.boot.domain.bean.Principal;
import com.cory.boot.domain.enumdef.EOperateType;
import com.cory.boot.domain.enumdef.EState;
import com.cory.boot.sys.service.SysService;
import com.cory.boot.utils.SpringUtils;
import com.cory.module.oplog.core.task.LogObjectTask;

/**
 * @author xiongcong
 * @date 2022-01-10
 */
public class OpLogClient {
    
    private static SysService sysService = SpringUtils.getBean(SysService.class);
    
    private static LogObjectTask logObjectTask = SpringUtils.getBean(LogObjectTask.class);
//    private static LogAttributesTask logAttributesTask = SpringUtils.getBean(LogAttributesTask.class);

    
    public static void log4Save(Object object) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        logObjectTask.log4Type(principal, object, EOperateType.ADD);
    }
    
    public static void log4Update(Object oldObject, Object newObject) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        logObjectTask.log4Update(principal, oldObject, newObject);
    }
    
    public static void log4Delete(Object object) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        logObjectTask.log4Type(principal, object, EOperateType.DELETE);
    }
    
    public static void log4State(Object object, Integer state) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        if (state == EState.DELETE.key) {
            logObjectTask.log4Type(principal, object, EOperateType.DELETE);
        }
        if (state == EState.ENABLE.key) {
            logObjectTask.log4Type(principal, object, EOperateType.ENABLE);
        }
        if (state == EState.DISABLE.key) {
            logObjectTask.log4Type(principal, object, EOperateType.DISABLE);
        }
        
    }
    
    public static void addLog(Class<?> clazz, Object objectId, String operateName) {
        addLog(clazz, objectId, operateName, null);
    }
    
    public static void addLog(Class<?> clazz, Object objectId, String operateName, String remarks) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        logObjectTask.addLog(principal, clazz, objectId, null, operateName, remarks);
    }
    
    public static void addLog(Class<?> clazz, Integer objectId, Integer operateId, String operateName) {
        addLog(clazz, objectId, operateId, operateName, null);
    }
    
    public static void addLog(Class<?> clazz, Integer objectId, Integer operateId, String operateName, String remarks) {
        Principal principal = sysService.getPrincipal();
        if (principal == null) {
            return;
        }
        
        logObjectTask.addLog(principal, clazz, objectId, operateId, operateName, remarks);
    }
    
//    public static void log4Update(String objectAlias, String objectName, Integer objectId, Integer userId, String userName, 
//            List<AttributeModel> attributes) {
//        Oplog log = new Oplog(objectAlias, objectName, objectId, userId, userName, EOpLogType.UPDATE.key, null);
//        
//        logAttributesTask.log(log, attributes);
//    }
}
